package com.caffeinatedbliss.nanowrimo;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.*;
import java.text.MessageFormat;

public class ThreeByFiveCardGraph implements DynamicImageGenerator {
  private static final int CURVE_RADIUS = 15;
  public static final int WIDTH = 500;
  public static final int HEIGHT = 300;

  public static final Color LIGHTEST_GREY = new Color(0xf0, 0xf0, 0xf0);
  public static final Color NOT_SO_LIGHTEST_GREY = new Color(0xbb, 0xbb, 0xdd);
  private static final GradientPaint GRADIENT = new GradientPaint(CURVE_RADIUS, CURVE_RADIUS,
    LIGHTEST_GREY, 2 * CURVE_RADIUS, CURVE_RADIUS * 7, NOT_SO_LIGHTEST_GREY, false);

  public static final Color GREY_BORDER = new Color(0xbb, 0xbb, 0xbb);
  public static final Color NAME_COLOR = new Color(0x66, 0x66, 0x66);
  public static final Color DK_GREY_BORDER = new Color(0x99, 0x99, 0x99);
  private static final GradientPaint OUTSIDE_GRADIENT = new GradientPaint(CURVE_RADIUS, CURVE_RADIUS,
    GREY_BORDER, 2 * CURVE_RADIUS, CURVE_RADIUS * 7, DK_GREY_BORDER, false);

  public static final Color GRAPH_BACKGROUND = new Color(0xee, 0xee, 0xee);
  public static final Color LT_RED_INK = new Color(0xff, 0xcc, 0xcc);
  public static final Color LT_GREEN_INK = new Color(0xcc, 0xff, 0xcc);
  public static final Color GREEN_INK = new Color(0x00, 0xcc, 0x00);

  private BufferedImage graphImage;
  private static final Font DAY_TEXT_FONT = new Font("dialog", Font.PLAIN, 9);
  private static final Font NAME_TEXT_FONT = new Font("dialog", Font.PLAIN, 10);
  private static final Font TEXT_FONT = new Font("dialog", Font.BOLD, 11);
  private NanowrimoComputer computer;

  public ThreeByFiveCardGraph(int[] amounts, int goal, String user, int year, String wordcountFormat) {
    computer = new NanowrimoComputer(amounts, goal);
    init(user, year, wordcountFormat);
  }

  private void init(String user, int year, String wordcountFormat) {
    graphImage = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = (Graphics2D) graphImage.getGraphics();

    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.setColor(Color.WHITE);
    g.fillRect(0,0,WIDTH,HEIGHT);
    g.setColor(Color.lightGray);
    g.drawRect(0,0,WIDTH-1,HEIGHT-1);
//    drawFilledRectangleWithCurvedCorners(g);
//    drawWordCount(g, wordcountFormat, computer.getCurrent(), computer.getPercentComplete(), computer.getDaysLeftNaNoWriMo());
    drawName(g, user);

    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
//    drawPercentCompleteBar(g);
//    drawDetailedBarChart(g, computer.getHistoryCounts(), computer.getExpectedPerDayAmount(),
//      computer.getOutliers(), year);

    g.dispose();
    computer = null;
  }

  private void drawName(Graphics2D g, String name) {
    g.setColor(NAME_COLOR);
    drawCenteredText(g, NAME_TEXT_FONT, name, -3);
  }

  private void drawCenteredText(Graphics g, Font textFont, String aTextString, int yPos) {
    g.setFont(textFont);
    FontMetrics fm = g.getFontMetrics(textFont);
    int ascent = fm.getAscent();
    int descent = fm.getDescent();
    int totalLineHeght = ascent + descent;
    int wordWidth = fm.stringWidth(aTextString);
    int leftEdge = (WIDTH - wordWidth) / 2;
    g.drawString(aTextString, leftEdge, yPos + totalLineHeght);
  }

  public BufferedImage getGraphImage() {
    return graphImage;
  }
}
